﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NTQOnlineExam.Services.DBCommand.Examination;
using NTQOnlineExam.Services.Infrastructure;
using NTQOnlineExam.Services.Models;
using NTQOnlineExam.Services.ReportService.Account;

namespace NTQOnlineExam.Services.ReportService.Examination
{
    public class ExaminationReportService : IExaminationReportService
    {
        private readonly ICommandExecutor _executor;
        private readonly IAccountReportService _accountReportService;

        public ExaminationReportService(ICommandExecutor executor, IAccountReportService accountReportService)
        {
            _executor = executor;
            _accountReportService = accountReportService;
        }

        public IList<ExaminationDTO> GetExams(int userId, int termId, int pageIndex, int pageSize, string sorting, out int totalRecord)
        {
            var result = _executor.Execute(new SearchExamsDbCommand(userId, termId, pageIndex, pageSize, sorting));
            if (result == null)
            {
                totalRecord = 0;
                return new List<ExaminationDTO>();
            }
            totalRecord = result.Count;
            return result.Items;
        }

        public IList<ExamInformationDTO> GetExamInformation(int termId)
        {
            return _executor.Execute(new GetExamInfoByTermIdDbCommand(termId));
        }

        public SearchResult<ExaminationDTO> SearchExamsForCurrentUser(SearchCriteria.SearchCriteria criteria, int pageSize, int pageIndex)
        {
            var user = _accountReportService.GetCurrentUser();
            if (user == null) return new SearchResult<ExaminationDTO>(new List<ExaminationDTO>(), 0);
            return _executor.Execute(new SearchExamByCriteriaDbCommand(criteria, (int)user.ProviderUserKey, pageSize, pageIndex));
        }

        public ExaminationDTO GetById(int examId)
        {
            return _executor.Execute(new GetExamByIdDbCommand(examId));
        }
    }
}
